The name of the file and the name of the class must coincide
exactly. For example the Queue
class should be
in a file named Queue.java
.
Note that Java is case-sensitive (even though Windows NT
really isn't).
In addition, each "word" within a class name should start
with an uppercase letter.
For example, TextMessage
and
SimpleTrafficMonitor
are both appropriate class names.
Further, each "word" within a variable name should start
with an uppercase letter.
For example, importantMessage
and
campusMonitor
are both appropriate variable names.
In general, even single-character variable names should be lowercase. However, in some situations, mathematical notation uses uppercase letters. In such situations, uppercase variable names may be used. For example, matrices are often written using uppercase letters. So, an expression like (b = A*x) would be appropriate.
Variable names like aaa
are not appropriate.
Index variables and counters can, however, have names like
i
and j
.
The only exception to this rule is that all constructors must
be listed first. For example, in a Queue
class, the
constructor(s) should be first, the pop
method should
be second, and the push
method should be third.
/** * A Queue of Object elements. * * @version 0.0 * @author CS Dept., James Madison University * * This work complies with the JMU Honor Code */ public class Queue { private int size; private Node back, front; /** * Construct a new (empty) Queue. */ public Queue() { front = null; size = 0; } /** * Pop an Object off of the front of this Queue. * * @return The Object on the front of this Queue */ public Object pop() { Object value; if (front != null) { value = front.value; front = front.next; size--; } else { value = null; } return value; } /** * Push an Object onto the back of this Queue. * * @param o The Object to push */ public void push(Object o) { Node temp; temp = new Node(); temp.value = o; temp.next = null; if (front == null) { front = temp; } else { back.next = temp; } back = temp; size++; } /** * Get the number of elements in this Queue. * * @return The number of elements */ public int size() { return size; } }
So, class variables should be declared at the top of the class (before any methods are declared) and variables that are local to a method should be declared at the top of that method (before any other lines of code). The only exceptions to this rule are:
In the constructor of a derived class the call to the constructor of the parent class must come first.
Index variables in loops may be declared locally.
For example, variables of type double
should be declared
before variables of type int
which should, in turn,
be declared before variables of type Node
.
For example, you must not have statements like
int i = 0;
. (Though this is inconvenient in
some situations, it prevents a common mistake made by beginning
programmers.)
This comment should describe the complete class (rather than the methods in the class).
This comment should describe the methods, its parameters, and its return value.
javadoc
is a program (written in Java) that creates external documentation
(in HTML) from block comments. All of the Java documentation was,
in fact, created this way. javadoc block comments start with
/**
and end with */
.
/** * This is where the description of the class goes. * You can include HTML tags if you would like. * * @author Author's name * @version Version number * * * This work complies with the JMU Honor Code. */ public class ClassName { /** * This is where the description of the method goes. * * @param param1 Description of the first parameter * @param param2 Description of the second parameter * @return Description of what is returned */ public ReturnType methodName(Type1 param1, Type2 param2) { } }
//
rather than /* ... */
.
This isn't a requirement but it will make your life easier since you
can't nest comments inside of /* ... */
. There is
nothing more annoying then trying to "comment out" a section of code
while you are debugging and being unable to because it contains
block comments.
* * This work complies with the JMU Honor Code. *
@author
element.
No particular indentation scheme is required but you must indent your code in a meaningful and consistent way.
Copyright 2020